package com.group.eams.controller;






import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.group.eams.pojo.dto.StorageQueryDto;
import com.group.eams.pojo.entity.Storage;
import com.group.eams.service.StorageService;
import com.group.pojo.dto.BatchDeleteDto;
import com.group.result.Result;
import com.group.result.ResultEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;




/**
 * 仓库表(Storage)表控制层
 *
 * @author 
 * @since 2024-10-30 12:58:29
 */
@RestController
@RequestMapping("/storage")
public class StorageController  {
    /**
     * 服务对象
     */
    @Autowired
    private StorageService storageService;

    @RequestMapping(value = "/list",method = RequestMethod.GET)
    public Result list(StorageQueryDto storageQueryDto){

        Page<Storage> page = new Page<>(storageQueryDto.getPage(), storageQueryDto.getLimit());

        LambdaQueryWrapper<Storage> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(!StrUtil.isEmpty(storageQueryDto.getName()),
                Storage::getName, storageQueryDto.getName());

        Page<Storage> storagePage = storageService.page(page, queryWrapper);
        return Result.buildSuccess(storagePage.getTotal(), storagePage.getRecords());

    }


    @RequestMapping(value = "/findById",method = RequestMethod.GET)
    public Result findById(@RequestParam Long id){
        return Result.buildSuccess(storageService.getById(id));
    }


    @RequestMapping(value = "/findAll",method = RequestMethod.GET)
    public Result findAll() {
        return Result.buildSuccess(storageService.list());
    }


    @RequestMapping(value = "/add",method = RequestMethod.POST)
    public Result add(@RequestBody Storage storage){
        storageService.save(storage);
        return Result.buildSuccess();
    }


   @RequestMapping(value = "/update",method = RequestMethod.POST)
   public Result update(@RequestBody Storage storage){
       LambdaUpdateWrapper<Storage> queryWrapper = new LambdaUpdateWrapper<>();
       queryWrapper.eq(Storage::getId, storage.getId());
       boolean result = storageService.update(storage, queryWrapper);
       return result ? Result.buildSuccess() : Result.buildFail(ResultEnum.OPTION_FAIL);

   }


   @RequestMapping(value = "/delete",method = RequestMethod.GET)
   public Result delete(@RequestParam Long id){
        LambdaQueryWrapper<Storage> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Storage::getId, id);
        boolean result = storageService.remove(queryWrapper);
        return result ? Result.buildSuccess() : Result.buildFail(ResultEnum.OPTION_FAIL);
   }


   @RequestMapping(value = "/batchRemove",method = RequestMethod.POST)
   public Result batchRemove(@RequestBody BatchDeleteDto batchDeleteDto){

        LambdaQueryWrapper<Storage> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.in(Storage::getId, batchDeleteDto.getIds());
        boolean result = storageService.remove(queryWrapper);
        return result ? Result.buildSuccess() : Result.buildFail(ResultEnum.OPTION_FAIL);
    }

}

